package com.blog.dao.impl;

import com.blog.dao.FolloweeDao;
import com.blog.pojo.Followee;
import com.blog.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class FolloweeDaoImpl implements FolloweeDao {
    private QueryRunner queryRunner = new QueryRunner( DruidUtils.getDataSource());
    @Override
    public int addFollowee(Followee followee) {
        String sql="INSERT into followee(uid,followeeId) VALUES(?,?)";
        Object[] params = {followee.getuId(),followee.getFolloweeId()};
        try {
            int add = queryRunner.update(sql, params);
            return add;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Followee> findByFolloweeId(int followeeId) {
        String sql="SELECT * from followee  WHERE followeeId=?";
        Object[] params = {followeeId};
        try {
            List<Followee> followeeList = queryRunner.query(sql, new BeanListHandler<Followee>(Followee.class), params);
            return followeeList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Followee findByUserIdAndFolloweeId(int uId,int followeeId) {
        String sql="SELECT * from followee  WHERE uid=? and followeeId=?";
        Object[] params = {uId,followeeId};
        try {
            Followee followee = queryRunner.query(sql, new BeanHandler<>(Followee.class), params);
            return followee;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delete(int uId, int followeeId) {
        String sql="DELETE from followee  WHERE uid=? and followeeId=?";
        Object[] params = {uId,followeeId};
        try {
            int delete = queryRunner.update(sql, params);
            return delete;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //粉丝列表
    @Override
    public List<Followee> findFanList(int followeeId) {
        String sql="SELECT * from followee  WHERE followeeId=?";
        Object[] params = {followeeId};
        try {
            List<Followee> followeeList = queryRunner.query(sql, new BeanListHandler<Followee>(Followee.class), params);
            return followeeList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int findFanCount(int followeeId) {
        String sql="SELECT count(*) from followee  WHERE followeeId=?";
        Object[] params = {followeeId};
        try {
            long count = queryRunner.query(sql, new ScalarHandler<>(), params);
            return  (int)count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //关注总数
    @Override
    public int findFolloweeCount(int uId) {
        String sql="SELECT count(*) from followee  WHERE uid=?";
        Object[] params = {uId};
        try {
            long count = queryRunner.query(sql, new ScalarHandler<>(), params);
            return (int)count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Followee> findFolloweeList(int uId) {
        String sql="SELECT * from followee  WHERE uid=?";
        Object[] params = {uId};
        try {
            List<Followee> followeeList = queryRunner.query(sql, new BeanListHandler<Followee>(Followee.class), params);
            return followeeList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int addFolloweeCount(int id) {
        String sql="update users set followeeCount=followeeCount+1 where id=?";
        Object[] params={id};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int reduceFolloweeCount(int id) {
        String sql="update users set followeeCount=followeeCount-1 where id=?";
        Object[] params={id};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int addFanCount(int id) {
        String sql="update users set fanCount=fanCount+1 where id=?";
        Object[] params={id};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int reduceFanCount(int id) {
        String sql="update users set fanCount=fanCount-1 where id=?";
        Object[] params={id};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
